<!DOCTYPE html>
<title>Mixing response using ServiceWorker</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/get-host-info.js"></script>
<script src="resources/test-helpers.js"></script>
<body>
<script>
// This test cannot be upstreamed to WPT because the behavior of mixing
// multiple resources are not defined in the spec. See
// https://github.com/slightlyoff/ServiceWorker/issues/703 for details.

function create_failure_audio_test(frame, url) {
  return new Promise(function(resolve, reject) {
      var audio = frame.contentWindow.document.createElement('audio');
      audio.oncanplay = function() {
        reject('canplay event should not be fired. url: ' + url);
      };
      audio.onerror = resolve;
      audio.src = url;
      frame.contentWindow.document.body.appendChild(audio);
    });
}

function create_success_audio_test(frame, url) {
  return new Promise(function(resolve, reject) {
      var audio = frame.contentWindow.document.createElement('audio');
      audio.oncanplay = resolve;
      audio.onerror = function(e) {
        reject('error event should not be fired. url: ' + url);
      };
      audio.src = url;
      frame.contentWindow.document.body.appendChild(audio);
    });
}

// Creates a test case to check the following behavior:
// 1. The audio element sends the first request.
//  - If |original| is 'same', the request is same-origin request.
//  - If |original| is 'cross', the request is cross-origin request.
// 2. The Service Worker may intercept the request depending on |first_byte|.
//  - If |first_byte| is '', the SW doesn't intercept, and the native server
//    returns the all data response and the test finishes.
//  - If |first_byte| is 'gen', the SW generates and returns the first byte
//    response 'O'.
//  - If |first_byte| is 'same', the SW does a fetch to
//    'service-worker-mixed-response.php' in the same origin server and returns
//    the response.
//  - If |first_byte| is 'cross', the SW does a fetch to
//    'service-worker-mixed-response.php' in the cross origin server and
//    returns the response.
// 3. The element sends the second request with "Range: bytes=1-" header.
// 4. The Service Worker may intercept the request depending on |second_byte|.
//  - If |second_byte| is '', the SW doesn't intercept, and the native server
//    returns the remaining data response and the test finishes.
//  - If |second_byte| is 'gen', the SW generates and returns the second byte
//    response 'g'.
//  - If |second_byte| is 'same', the SW does a fetch to
//    'service-worker-mixed-response.php' in the same origin server and returns
//    the response.
//  - If |second_byte| is 'cross', the SW does a fetch to
//    'service-worker-mixed-response.php' in the cross origin server and
//    returns the response.
// 5. The element sends the third request with "Range: bytes=2-" header.
// 6. The native server returns the remaining data.
//
// When the audio element recieves the first response, it remembers the original
// URL of it. And when it receives the succeeding response, it checks the origin
// of the new response. If the origin is not same as the origin of the first
// response, the response must be treated as an error.

var request_num = 1;
function audio_test(frame, original, first_byte, second_byte, expect_success) {
  var url;
  var HOST_INFO = get_host_info();
  var AUDIO_PATH = '/media/resources/load-video.php?' +
                   'name=../../../../media/content/silence.oga&type=audio/ogg';
  if (original == 'same') {
    url = HOST_INFO['HTTP_ORIGIN'] + AUDIO_PATH;
  } else if (original == 'cross') {
    url = HOST_INFO['HTTP_REMOTE_ORIGIN'] + AUDIO_PATH;
  }
  url += '&SW_FIRST=' + first_byte + '&SW_SECOND=' + second_byte;
  url += '&prevent_cache=' + request_num;
  request_num += 1;
  if (expect_success) {
    return create_success_audio_test(frame, url);
  } else {
    return create_failure_audio_test(frame, url);
  }
}

promise_test(function(t) {
    var SCOPE = 'resources/blank.html?/service-worker-mixed-response';
    var SCRIPT = 'resources/service-worker-mixed-response-worker.js';
    var frame;
    return service_worker_unregister_and_register(t, SCRIPT, SCOPE)
      .then(function(registration) {
          return wait_for_state(t, registration.installing, 'activated');
        })
      .then(function() { return with_iframe(SCOPE); })
      .then(function(f) {
          frame = f;
          return Promise.all([
              audio_test(f, 'same', '', '', true),
              audio_test(f, 'same', 'gen', '', false),
              audio_test(f, 'same', 'gen', 'gen', false),
              audio_test(f, 'same', 'gen', 'same', false),
              audio_test(f, 'same', 'gen', 'cross', false),
              audio_test(f, 'same', 'same', '', true),
              audio_test(f, 'same', 'same', 'gen', false),
              audio_test(f, 'same', 'same', 'same', true),
              audio_test(f, 'same', 'same', 'cross', false),
              audio_test(f, 'same', 'cross', '', false),
              audio_test(f, 'same', 'cross', 'gen', false),
              audio_test(f, 'same', 'cross', 'same', false),
              audio_test(f, 'same', 'cross', 'cross', false),
              audio_test(f, 'cross', '', '', true),
              audio_test(f, 'cross', 'gen', '', false),
              audio_test(f, 'cross', 'gen', 'gen', false),
              audio_test(f, 'cross', 'gen', 'same', false),
              audio_test(f, 'cross', 'gen', 'cross', false),
              audio_test(f, 'cross', 'same', '', false),
              audio_test(f, 'cross', 'same', 'gen', false),
              audio_test(f, 'cross', 'same', 'same', false),
              audio_test(f, 'cross', 'same', 'cross', false),
              audio_test(f, 'cross', 'cross', '', true),
              audio_test(f, 'cross', 'cross', 'gen', false),
              audio_test(f, 'cross', 'cross', 'same', false),
              audio_test(f, 'cross', 'cross', 'cross', true)]);
        })
      .then(function() {
          frame.remove();
          return service_worker_unregister_and_done(t, SCOPE);
        });
  }, 'Tests for Service Worker generated mixed responses.');
</script>
</body>
